import Vue from 'vue'
import VueRouter from 'vue-router'
import NProgress from 'nprogress'
import store from '@/store/store'
import { routeMapBase } from './routeMapBase.js'
import { routeMapPages } from './routeMapPages.js'
import util from '@/libs/util';

let { cookies } = util

Vue.use(VueRouter)

const router = new VueRouter({
	routes: [...routeMapBase,...routeMapPages]
 })

// 路由勾子必须挂在实例上
router.beforeEach((to, from, next) => {
	let token = cookies.get('token')
	NProgress.start()
	
	//刷新页面时，要重新加载菜单
	if(from.path == '/' ){
		// 不要在这里加载
		// store.dispatch('getMenuList') //加载菜单进
	}
	
	//以下是不判断登录的,用于开发测试
	//每次切换路由把当前路由信息放进vuex中当的tab列表中
	// store.dispatch('tab/WORKTAB_ROUTEADD', {
	// 	to: {
	// 		name: to.name ? to.name : '',
	// 		tabname: (to.meta && to.meta.tabname) ? to.meta.tabname : '',
	// 		path: to.path
	// 	},
	// 	from: {
	// 		name: from.name ? from.name : '',
	// 		tabname: (from.meta && from.meta.tabname) ? from.meta.tabname : '',
	// 		path: from.path
	// 	}
	// })
	// next()
	
	//以下是正常的判断是否逻辑
	if (token) {
        // console.log("有token：",token)
		if (to.path == '/login') {//如果有登录的情况下进入登录，就直接跳到首页
			next({ path:'/' })
		} else {
			// 每次切换路由把当前路由信息放进vuex中当的tab列表中
			store.dispatch('tab/WORKTAB_ROUTEADD', {
				to: {
					name: to.name ? to.name : '',
					tabname: (to.meta && to.meta.tabname) ? to.meta.tabname : '',
					path: to.path
				},
				from: {
					name: from.name ? from.name : '',
					tabname: (from.meta && from.meta.tabname) ? from.meta.tabname : '',
					path: from.path
				}
			})
			next()
		}
	} else {
		console.log("无token：",token)
		if (to.path == '/login') {// ("目标是login，继续")
			next()
		} else {// ("目标不是login，跳到login")
			next('/login')
		}

	}
    NProgress.done();
})

router.afterEach((to, after) => {
	NProgress.done()
})

export default router
